package com.example.bestudentsystem.demos.web.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bestudentsystem.demos.web.mapper.RateMapper;
import com.example.bestudentsystem.demos.web.pojo.RateRecord;
import com.example.bestudentsystem.demos.web.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class rateController {
    @Autowired
    RateMapper rateMapper;
    // 添加评教记录
    @PostMapping("/rate")
    public int addRate(@RequestBody RateRecord rateRecord){
        return rateMapper.insert(rateRecord);
    }
    // 分页查询评教记录
    @GetMapping("/rate")
    public IPage<RateRecord> getRate(@RequestParam("pageNum") Integer pageNum,
                                     @RequestParam("pageSize") Integer pageSize){
        IPage<RateRecord> iPage = new Page<>(pageNum,pageSize);
        return rateMapper.selectRateByPage(iPage);
    }
    // 更新评教的状态
    @PutMapping("/rate/{sno}/{cno}/{sta}")
    public int updateRate(@PathVariable("sno") String sno,
                          @PathVariable("cno") String cno,
                          @PathVariable("sta") String status){
        return rateMapper.updateStatus(sno,cno,status);
    }
    @PutMapping("/rates/{sta}")
    public int updateRate(@PathVariable("sta") String status
                           ,@RequestParam("sno") String sno){
        return rateMapper.updateAllStatus(status,sno);
    }
    //更新审核状态
    @PutMapping("/rateCheck/{sno}/{cno}/{israte}")
    public Result updateRate(@PathVariable("sno") List<String> snos,
                             @PathVariable("cno") List<String> cnos,
                             @PathVariable("israte") String israte) {
        for(String cno : cnos){
            for(String sno : snos){
                rateMapper.updateCheck(sno,cno,israte);
            }
        }
        return Result.success("更新成功");
    }
    // 根据sno和cno查询评教记录
    @GetMapping("/rate/{sno}/{cno}")
    public RateRecord getRateBySnoAndCno(@PathVariable("sno") String sno,
                                         @PathVariable("cno") String cno){
        return rateMapper.selectRate(sno,cno);
    }
    @GetMapping("/checkRate")
    public IPage<RateRecord> getRateByTno(@RequestParam("pageNum") Integer pageNum,
                                          @RequestParam("pageSize") Integer pageSize,
                                          @RequestParam("tno") String tno,
                                          @RequestParam("cno") String cno){
        IPage<RateRecord> iPage = new Page<>(pageNum,pageSize);
        return rateMapper.selectCheckRate(iPage,tno,cno);
    }
    // 修改评教记录
    @PutMapping("/rate")
    public int updateRate(@RequestBody RateRecord rateRecord){
        return rateMapper.updateRate(rateRecord);
    }
    // 删除评教记录
    @DeleteMapping("/rate/{sno}/{cno}")
    public int deleteRate(@PathVariable("sno") String sno,
                          @PathVariable("cno") String cno){
        return rateMapper.updateStatus(sno,cno,"评教中");
    }
    @GetMapping("/rateByCno")
    public IPage<RateRecord> getRateByCno(@RequestParam("pageNum") Integer pageNum,
                                          @RequestParam("pageSize") Integer pageSize,
                                          @RequestParam("cno") String cno){
        IPage<RateRecord> iPage = new Page<>(pageNum,pageSize);
        return rateMapper.selectRateByCno(iPage,cno);
    }
}
